package threads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import rm2.DRSserverinit;
import rm2.UDPDatagram;
import exception.CustomerNotExistsException;
import exception.ItemNotExistsException;

public class CheckInfo {
	DRSserverinit drSserverinit;

	public CheckInfo(DRSserverinit drSserverinit)
	{		
		this.drSserverinit=drSserverinit;
	}
	
	public boolean CheckBoughtItem(String customer_id, String bought_item_id, int bought_number)
	{
		String message = "unsuccessful";
		try
		{
			File file = new File(this.drSserverinit.input_folder_path  + File.separator + "CustomerFile" + File.separator + customer_id);
			String customer="";
	
			String[] token_list, item_list;
			String reserved="";
			int numberOfItems = 0;
			String itemInfo = "";
			
			if (file.exists())
			{			
				BufferedReader br = new BufferedReader(new FileReader(file));
				String line;
				int ind = 0, line_count=1;
				
				while ( (line = br.readLine()) != null)
				{
					token_list = line.split(":");
							if (line_count == 1)
							{
								//read the customer ID
								customer = token_list[1].trim();
								//System.out.println(customer);
							}
							
							if(line_count == 2)
							{
								// Read the item information								
								itemInfo =  token_list[1].trim();
								item_list = itemInfo.split(",");
								
								for(int i = 0; i<item_list.length; i++)
								{
									if(item_list[i].contains(bought_item_id))
									{
										//int number;
										//item_list[i].sub
									numberOfItems = Integer.parseInt(item_list[i].replace(bought_item_id, "").trim());
									if(numberOfItems>bought_number)
										return true;
									else
										return false;
									//System.out.println(numberOfItems);
									//	numberOfItems = numberOfItems + number;										
									//	item_list[i] = item_id + " " + Integer.toString(numberOfItems);									
									
									}
								
								}
							}
							line_count++;							
						}				
				br.close();
			}
			else
				throw new CustomerNotExistsException(customer_id  +" --> " + ": " + " Customer does not exist.");
			//End of FileExist
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
		}

		
		
		return false;
		
	}
	
	public boolean CheckDesiredItem(String customer_id, String desired_item_id, int desired_number)
	{
		try
		{
			File file = new File(this.drSserverinit.input_folder_path  + File.separator + "ItemList");
			String customer="";
	
			String[] token_list, item_list;
			String reserved="";
			int numberOfItems = 0;
			String itemInfo = "";
			
			if (file.exists())
			{			
				BufferedReader br = new BufferedReader(new FileReader(file));
				String line;
				int ind = 0, line_count=1;
				
				while ( (line = br.readLine()) != null)
				{
					if(line.contains(desired_item_id))
					{
						numberOfItems = Integer.parseInt(line.replace(desired_item_id, "").trim());
						if(numberOfItems>desired_number)
							return true;
						else
							return false;
						//System.out.println(numberOfItems);
					}
					//token_list = line.split(":");
							
					line_count++;							
				}				
				br.close();
			}
			else
				throw new CustomerNotExistsException("ItemList"  +" --> " + ": " + "does not exist.");
			//End of FileExist
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
		}

		
		
		return false;
		
	}
}
